//
//  GDLocationManager.m
//  baseVC
//
//  Created by 陶磊 on 2019/12/16.
//  Copyright © 2019 QHT. All rights reserved.
//

#import "GDLocationManager.h"

@interface GDLocationManager ()
@property (nonatomic, strong) AMapLocationManager *locationManager; // 高德定位
@end

@implementation GDLocationManager {
    AMapLocationManager *_locationManager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

+ (GDLocationManager *)manager
{
    return [[self alloc] init];
}

- (void)setup
{
    self.locationManager = [[AMapLocationManager alloc] init];
    // 带逆地理信息的一次定位（返回坐标和地址信息）
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    // 定位超时时间，最低2s，此处设置为2s
    self.locationManager.locationTimeout = 2;
    // 逆地理请求超时时间，最低2s，此处设置为2s
    self.locationManager.reGeocodeTimeout = 2;
    
}

+ (void)saveLocation
{
    [self requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        if (error)
        {
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            
            if (error.code == AMapLocationErrorLocateFailed)
            {
                return;
            }
        }
        
        NSLog(@"location:%f, %f", location.coordinate.latitude, location.coordinate.longitude);
    
        // 保存定位信息
        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%f", location.coordinate.longitude] forKey:baseReceiveLongitude]; //精度
        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%f", location.coordinate.latitude] forKey:baseReceiveLatitude]; //未读
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:baseReceiveAddress];
        
    }];
}


+ (BOOL)requestLocationWithReGeocode:(BOOL)withReGeocode completionBlock:(AMapLocatingCompletionBlock)completionBlock
{
    // 带逆地理（返回坐标和地址信息）。将下面代码中的 YES 改成 NO ，则不会返回地址信息。
    GDLocationManager *manager = [self manager];
    return [manager.locationManager requestLocationWithReGeocode:withReGeocode completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        manager; // 避免提前释放
        
        if (regeocode)
        {
            NSLog(@"reGeocode:%@", regeocode.formattedAddress);
            // 保存定位地址
            [[NSUserDefaults standardUserDefaults] setObject:regeocode.formattedAddress forKey:baseReceiveAddress];
            
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            if (regeocode.country.length) {
                [dic setObject:regeocode.country forKey:@"country"];
            }
            if (regeocode.province.length) {
                [dic setObject:regeocode.province forKey:@"province"];
            }
            if (regeocode.city.length) {
                [dic setObject:regeocode.city forKey:@"city"];
            }
            if (regeocode.district.length) {
                [dic setObject:regeocode.district forKey:@"district"];
            }
            if (regeocode.citycode.length) {
                [dic setObject:regeocode.citycode forKey:@"citycode"];
            }
            if (regeocode.adcode.length) {
                [dic setObject:regeocode.adcode forKey:@"adcode"];
            }
            if (regeocode.street.length) {
                [dic setObject:regeocode.street forKey:@"street"];
            }
            if (regeocode.number.length) {
                [dic setObject:regeocode.number forKey:@"number"];
            }
            if (regeocode.POIName.length) {
                [dic setObject:regeocode.POIName forKey:@"POIName"];
            }
            if (regeocode.AOIName.length) {
                [dic setObject:regeocode.AOIName forKey:@"AOIName"];
            }
            [USERDEFAULT setObject:dic forKey:baseReGeocodeDic];
        }
        completionBlock(location, regeocode, error);
    }];
}

// 获得定位信息
+ (NSDictionary *)receiveLocationDic
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    
    NSString *receiveLongitude = [[NSUserDefaults standardUserDefaults] objectForKey:baseReceiveLongitude]; // 经度
    NSString *receiveLatitude = [[NSUserDefaults standardUserDefaults] objectForKey:baseReceiveLatitude]; // 纬度
    NSString *receiveAddress = [[NSUserDefaults standardUserDefaults] objectForKey:baseReceiveAddress]; // 地址
    
    dic[receiveLongitude] = receiveLongitude ? receiveLongitude : @"";
    dic[receiveLatitude] = receiveLatitude ? receiveLatitude : @"";
    dic[receiveAddress] = receiveAddress ? receiveAddress : @"";
    
    return dic;
}




@end
